package boun.swe.bob.service;

import boun.swe.bob.entities.Bob_Event;
import boun.swe.bob.entities.User;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.apache.commons.lang.xwork.StringEscapeUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: ememory
 * Date: 27.Ara.2010
 * Time: 18:01:12
 * To change this template use File | Settings | File Templates.
 */

public class Bob_EventController extends BaseController {
    @RequestMapping(method = RequestMethod.GET)
        public ModelAndView showPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
            Map<String, Object> myModel = Maps.newHashMap();
            String action = httpServletRequest.getParameter("action");
            if(!Strings.isNullOrEmpty(action)) {
                int eventId = Integer.parseInt(httpServletRequest.getParameter("event_id"));
                if("remove".equals(action)) {
                    bob_EventDao.LeaveEvent(eventId, getLoggedInUser(httpServletRequest).getUserId());
                } else if("add".equals(action)) {
                    bob_EventDao.JoinEvent(eventId, getLoggedInUser(httpServletRequest).getUserId());
                }
                try {
                    httpServletResponse.sendRedirect("/bob-web/service/event.htm?event_id=" + eventId);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));
            myModel.put("himself",true);
            Bob_Event myEvent = new Bob_Event();
            if(null != httpServletRequest.getParameter("event_id")){
                myEvent= bob_EventDao.findEventByEventId(Integer.parseInt(httpServletRequest.getParameter("event_id")));
                myModel.put("isJoined",bob_EventDao.IsUserEvent(Integer.parseInt(httpServletRequest.getParameter("event_id")),getLoggedInUser(httpServletRequest).getUserId()));
            }
            myModel.put("event",myEvent);
            myModel.put("topMembers",myEvent.getTopMembersList());
            myModel.put("topFeeds",myEvent.getTopFeedsList());
            myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));

            return new ModelAndView("event", "model", myModel);
        }

            @RequestMapping(method = RequestMethod.POST)
            public ModelAndView handlePost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
            Map<String, Object> myModel = Maps.newHashMap();
            String feedCountStr = httpServletRequest.getParameter("feedCount");
            int feedCount = 5;
            if(null != feedCountStr) {
                feedCount = Integer.parseInt(feedCountStr) + 5;
            }

            String tweet = httpServletRequest.getParameter("tweet");
            try {
                tweet = StringEscapeUtils.unescapeHtml(tweet);
            }
            catch (Exception exp) {
            }


            User user = getLoggedInUser(httpServletRequest);

            if (!Strings.isNullOrEmpty(tweet) && !tweet.equals("\r\n\r\n                ")) {
                compositeDao.addEventFeed(getLoggedInUser(httpServletRequest).getUserId(),Integer.parseInt(httpServletRequest.getParameter("event_id")),tweet);
            }


            Bob_Event my_event=bob_EventDao.findEventByEventId(Integer.parseInt(httpServletRequest.getParameter("event_id")));
            myModel.put("event",my_event);
            myModel.put("topMembers",my_event.getTopMembersList());
            myModel.put("topFeeds",my_event.getTopFeedsList());
            myModel.put("isJoined",bob_EventDao.IsUserEvent(Integer.parseInt(httpServletRequest.getParameter("event_id")),getLoggedInUser(httpServletRequest).getUserId()));
            myModel.put("himself", true);
            myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));

            return new ModelAndView("event", "model", myModel);

        }

}